#ifndef __FLASH_PANEL_H
#define __FLASH_PANEL_H

#include "stm32f7xx.h"                  // Device header
#include "sys.h"

#include "usbd_cdc.h"
#include "usbd_ctlreq.h"
//////////////////////////////////////////////////////////////////////////////////	 
//本程序只供学习使用，未经作者许可，不得用于其它任何用途
//ALIENTEK STM32开发板 
//FLASH_panel_ QPI模式驱动代码	   
//正点原子@ALIENTEK
//技术论坛:www.openedv.com
//创建日期:2016/7/18
//版本：V1.0
//版权所有，盗版必究。
//Copyright(C) 广州市星翼电子科技有限公司 2014-2024
//All rights reserved									  
////////////////////////////////////////////////////////////////////////////////// 	

//W25X系列/Q系列芯片列表	   
//W25Q80  ID  0XEF13
//W25Q16  ID  0XEF14
//W25Q32  ID  0XEF15
//W25Q64  ID  0XEF16	
//W25Q128 ID  0XEF17	
//W25Q256 ID  0XEF18
#define W25Q80 	0XEF13 	
#define W25Q16 	0XEF14
#define W25Q32 	0XEF15
#define W25Q64 	0XEF16
#define W25Q128	0XEF17
#define W25Q256 0XEFAA
#define GD25LQ255E 0x2050/////G1610FP103GG  6.1 EDP CASE USE
//#define GD25LQ255E 0xC860/////G1B12FP101FF USE

extern u16 FLASH_panel__TYPE;					//定义FLASH_panel_芯片型号		   
 
////////////////////////////////////////////////////////////////////////////////// 
//指令表
#define Flash_WriteEnable		0x06    //
#define Flash_WriteDisable		0x04 //
#define Flash_ReadStatusReg1		0x05 //
#define Flash_ReadStatusReg2		0x35 //
#define Flash_PR_Add					0xA0//
#define Flash_CR_Add					0xB0//
#define Flash_SR_Add					0xC0//
//#define Flash_ReadStatusReg2		0x0f//
#define Flash_BB_MAN_Block		0xA1
#define Flash_BBM_LUT_R			0xA5
#define Flash_ECC_FAIL_Page		0xA9
#define Flash_WriteStatusReg1    0x01// 
#define Flash_WriteStatusReg2    0x1f// 

#define Flash_Page_W 			0x02
#define Flash_Page_R			0x13
#define Flash_ReadData			0x03 //
#define Flash_ReadData1			0x0B //
#define Flash_FastReadData		0x6B //
#define Flash_FastReadDual		0x3B 
#define Flash_PageProgram		0x32 //
#define Flash_BlockErase		0xD8 //64k block erase
#define Flash_Block32kErase		0x52 //32k block erase
#define Flash_SectorErase		0x20 ///4k sector erase
#define Flash_ChipErase			0xC7 ///chip erase
#define Flash_PowerDown			0xB9 
#define Flash_ReleasePowerDown	0xAB 
#define Flash_DeviceID			0xAB 
#define Flash_ManufactDeviceID	0x90 
#define Flash_JedecDeviceID		0x9F 
#define Flash_Enable4ByteAddr    0xB7
#define Flash_Exit4ByteAddr      0xE9
#define Flash_SetReadParam		0xC0 
#define Flash_EnterQPIMode       0x38
#define Flash_ExitQPIMode        0xFF
#define Flash_Block_MAX			1023

#define	TCON_RST_ON    HAL_GPIO_WritePin(GPIOD, GPIO_PIN_11, GPIO_PIN_SET)
#define	TCON_RST_OFF    HAL_GPIO_WritePin(GPIOD, GPIO_PIN_11, GPIO_PIN_RESET)

#define	TCON_BIST_ON    HAL_GPIO_WritePin(GPIOG, GPIO_PIN_6, GPIO_PIN_SET)
#define	TCON_BIST_OFF    HAL_GPIO_WritePin(GPIOG, GPIO_PIN_6, GPIO_PIN_RESET)

#define	LCD_BL_EN_ON    HAL_GPIO_WritePin(GPIOE, GPIO_PIN_1, GPIO_PIN_SET)
#define	LCD_BL_EN_OFF    HAL_GPIO_WritePin(GPIOE, GPIO_PIN_1, GPIO_PIN_RESET)

#define	LCD_PWM_ON    HAL_GPIO_WritePin(GPIOE, GPIO_PIN_2, GPIO_PIN_SET)
#define	LCD_PWM_OFF    HAL_GPIO_WritePin(GPIOE, GPIO_PIN_2, GPIO_PIN_RESET)

#define Flash_CS_Periph_GPIO				    GPIOE
#define Flash_CS						        GPIO_PIN_4
//#define Flash_CS_HIGH						    HAL_GPIO_WritePin( Flash_CS_Periph_GPIO,  Flash_CS,GPIO_PIN_SET);
//#define Flash_CS_LOW							HAL_GPIO_WritePin( Flash_CS_Periph_GPIO,  Flash_CS,GPIO_PIN_RESET);

//#define	Flash_SPI_CS_L     HAL_GPIO_WritePin(Flash_CS_Periph_GPIO, Flash_CS, GPIO_PIN_RESET);
//#define	Flash_SPI_CS_H     HAL_GPIO_WritePin(Flash_CS_Periph_GPIO, Flash_CS, GPIO_PIN_SET);

#define	Flash_SPI_CS_L     HAL_GPIO_WritePin(Flash_CS_Periph_GPIO, Flash_CS, GPIO_PIN_RESET);
#define	Flash_SPI_CS_H     HAL_GPIO_WritePin(Flash_CS_Periph_GPIO, Flash_CS, GPIO_PIN_SET);

#define Flash_SCK_Periph_GPIO				    GPIOD
#define Flash_SCK						        GPIO_PIN_12
#define	Flash_SPI_SCK_L    HAL_GPIO_WritePin(Flash_SCK_Periph_GPIO, Flash_SCK, GPIO_PIN_RESET);
#define	Flash_SPI_SCK_H    HAL_GPIO_WritePin(Flash_SCK_Periph_GPIO, Flash_SCK, GPIO_PIN_SET);


#define	Flash_SPI_IO0_GPIO    GPIOD////GPIOD
#define	Flash_SPI_IO0_PIN    GPIO_PIN_13////13	11    ///3.7  USE
//#define	Flash_SPI_IO0_GPIO    GPIOG////GPIOD
//#define	Flash_SPI_IO0_PIN    GPIO_PIN_13////13	11 
#define	Flash_SPI_IO0_L    HAL_GPIO_WritePin(Flash_SPI_IO0_GPIO, Flash_SPI_IO0_PIN, GPIO_PIN_RESET);
#define	Flash_SPI_IO0_H    HAL_GPIO_WritePin(Flash_SPI_IO0_GPIO, Flash_SPI_IO0_PIN, GPIO_PIN_SET);

#define	Flash_SPI_IO1_GPIO    GPIOD////GPIOE
#define	Flash_SPI_IO1_PIN    GPIO_PIN_11///3  ////2.83  USE
//#define	Flash_SPI_IO1_GPIO    GPIOE////GPIOE
//#define	Flash_SPI_IO1_PIN    GPIO_PIN_3///3
#define	Flash_SPI_IO1_L    HAL_GPIO_WritePin(Flash_SPI_IO1_GPIO, Flash_SPI_IO1_PIN, GPIO_PIN_RESET);
#define	Flash_SPI_IO1_H    HAL_GPIO_WritePin(Flash_SPI_IO1_GPIO, Flash_SPI_IO1_PIN, GPIO_PIN_SET);

#define	Flash_SPI_IO2_GPIO    GPIOD
#define	Flash_SPI_IO2_PIN    GPIO_PIN_11
#define	Flash_SPI_IO2_L    HAL_GPIO_WritePin(Flash_SPI_IO2_GPIO, Flash_SPI_IO2_PIN, GPIO_PIN_RESET);
#define	Flash_SPI_IO2_H    HAL_GPIO_WritePin(Flash_SPI_IO2_GPIO, Flash_SPI_IO2_PIN, GPIO_PIN_SET);

#define	Flash_SPI_IO3_GPIO    GPIOG
#define	Flash_SPI_IO3_PIN    GPIO_PIN_13
#define	Flash_SPI_IO3_L   HAL_GPIO_WritePin(Flash_SPI_IO3_GPIO, Flash_SPI_IO3_PIN, GPIO_PIN_RESET);
#define	Flash_SPI_IO3_H   HAL_GPIO_WritePin(Flash_SPI_IO3_GPIO, Flash_SPI_IO3_PIN, GPIO_PIN_SET);

//#define	Flash_SPI_IO1_MISO_GPIO    GPIOE////GPIOE
//#define	Flash_SPI_IO1_MISO_PIN    GPIO_PIN_3///3
#define	Flash_SPI_IO1_MISO_GPIO    GPIOD////GPIOE
#define	Flash_SPI_IO1_MISO_PIN    GPIO_PIN_13///3           ///3.7  USE
#define	Flash_SPI_IO1_L    HAL_GPIO_WritePin(Flash_SPI_IO1_MISO_GPIO, Flash_SPI_IO1_MISO_PIN, GPIO_PIN_RESET);
#define	Flash_SPI_IO1_H    HAL_GPIO_WritePin(Flash_SPI_IO1_MISO_GPIO, Flash_SPI_IO1_MISO_PIN, GPIO_PIN_SET);
//-----------------------------------------------------------------------------------------
extern u32 VCP_Receive_True_num;   //单次Mura数据接收累计
extern u32 USB_Rx_Demura_Total_Num;//Mura数据接收全累计
extern u32 USB_Tx_Demura_Num;
extern u32 USB_Rx_Demura_Total_Num_Temp;
extern u32 USB_Rx_Demura_Num;
extern u32 USB_Rx_Demura;

extern u8  USB_Rx_Demura_End_Flag; //Demura数据全部完成标志
extern u8  DemuraIctype;  		  	 //识别IC型号
extern u8  Demura_OLEDModule_flag; //同型DDIC下的衍生项目
extern u32 Demura_START_FLAG;  		 //Demura 数据传输开始标志

extern u32 DemuraRecivedNum;       //统计已经接收的数据数量
extern u32 DemuraTransOnce;        //单笔Demura资料传输数据量
extern u8  Demura_first_package_flag;  //首次发送标志 2C/3C use

extern u16  DemuraSize_H8; //Hefei Gen demura define for record Demura size 
extern u8  DemuraSize_L8; //Hefei Gen demura define for record Demura size 
extern u8  DemuraAddr_1st;//Hefei Gen demura define for record Demura 1st addr 
extern u8  DemuraAddr_2nd;//Hefei Gen demura define for record Demura 2nd addr  




void FLASH_panel__Init(void);					//初始化FLASH_panel_
void FLASH_panel_spi_4byte_Enable(void);        /////使用SPI 4yte 模式
void FLASH_panel_spi_4byte_Disable(void);       /////退出SPI 4byte模式
void FLASH_panel__Qspi_Enable(void);			//使能QSPI模式
void FLASH_panel__Qspi_Disable(void);			//关闭QSPI模式
u32  FLASH_panel__ReadID(void);  	    		//读取FLASH ID
u8 FLASH_panel__ReadSR(u8 regno);             //读取状态寄存器 
void FLASH_panel__4ByteAddr_Enable(void);     //使能4字节地址模式
void FLASH_panel__Write_SR(u8 regno,u8 sr);   //写状态寄存器
void FLASH_panel__Write_Enable(void);  		//写使能 
void FLASH_panel__Write_Disable(void);		//写保护
//void FLASH_panel__Write_NoCheck(u8* pBuffer,u32 WriteAddr,u32 NumByteToWrite);//写flash,不校验
void FLASH_panel__Write_NoCheck(u8* pBuffer,u32 PageAddr,u16 CloAddr,u32 NumByteToWrite);//写flash,不校验   
//void FLASH_panel__Read(u8* pBuffer,u32 ReadAddr,u32 NumByteToRead);   //读取flash
void FLASH_panel__Read(u8* pBuffer,u32 PageAddr,u16 CloAddr,u32 NumByteToRead);  //读取flash
void FLASH_panel__Write(u8* pBuffer,u32 PageAddr,u16 CloAddr ,u32 NumByteToWrite);//写入flash
//void FLASH_panel__Write(u8* pBuffer,u32 WriteAddr,u32 NumByteToWrite);//写入flash
void FLASH_panel__Erase_Chip(void);    	  	//整片擦除
void FLASH_panel__Erase_Sector(u32 Dst_Addr);	//扇区擦除
void FLASH_panel__Wait_Busy(void);           	//
void FLASH_panel_DataToBuff(u8* pBuffer,u32 Addr,u32 NumByteToWrite);  //数据搬运到缓冲区
void FLASH_panel_Flash_Program(u32 Addr);  //数据搬运到缓冲区
void FLASH_panel_DataToBuff_Len(u8* pBuffer,u32 Addr,u32 NumByteToWrite);  //数据搬运到缓冲区
void FLASH_panel__Wait_Wel_Busy(void);
u8 FLASH_panel_FlashXX_Read_ECC_check(void);
u32 FLASH_panel_FlashXX_Read_BB_MAN_Block(u8* pBuffer);
u32 FLASH_panel_FlashXX_Read_ECC_Fail_Page(u8* pBuffer);
void FLASH_panel_flash_reset(void);
u32 FLASH_panel_FlashXX_Read_reg(u8 index,u8* pBuffer,u8 num);
u8 FLASH_panel_w25n01g_readBBLUT(u8* pBuffer, int lutsize);
void FLASH_panel__Wait_Wel_ready(void);

void FLASH_Panel_write_read_test(void);
void bin_flash(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data);


void SPI_Panel_DDIC(u8 address,u8 addr_h,u8 addr_L,u8 data,u8 dummy);
u8 SPI_Panel_DDIC_Read(u8 address,u8 addr_h,u8 addr_L,u8 dummy, u8 data);
void SPI_Panel_DDIC_long(u16 length,u8 para[],u8 offerset);
void SPI_Panel_DDIC_long_read(u16 length,u8 para[],u8 offerset,u8* buffer);
void SPI_Panel_DDIC_long_16bit(u16 length,u8 para[],u8 offerset);


#endif
